package com.mmy.service.plane.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mmy.controller.UtilConstants;
import com.mmy.dao.plane.TPlanePlanRepository;
import com.mmy.dao.plane.VPlanePlanRepository;
import com.mmy.domain.plane.TPlanePlan;
import com.mmy.domain.plane.VPlanePlan;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.ServiceCommon;
import com.mmy.service.plane.PlanePlanService;

@Service
public class PlanePlanServiceImpl implements PlanePlanService {
	@Autowired
	VPlanePlanRepository vPlanePlanRepository;
	@Autowired
	TPlanePlanRepository tPlanePlanRepository;
	@Autowired
	ServiceCommon common;

	/**
	 * 通过计划单ID查询
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public VPlanePlan findByID(int iPlanID) {
		// TODO Auto-generated method stub
		return vPlanePlanRepository.findByPlanID(iPlanID);
	}

	/**
	 * 函数功能说明： 跳转进入前台飞机机票页面 机票推荐模块 推荐由北京出发的机票信息 国内
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人Hanxl
	 * @date 创建日期2021.02.24 修改时间2021.03.17
	 */

	public List<VPlanePlan> findByZoneOutName(String strZoneOutName) {

		return vPlanePlanRepository.findByZoneOutName(strZoneOutName);
	}

	/**
	 * 函数功能说明： 跳转进入前台飞机机票页面 机票推荐模块 推荐由北京出发的机票信息 国外
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人Hanxl
	 * @date 创建日期2021.02.24 修改时间2021.03.17
	 */

	public List<VPlanePlan> findOutByZoneOutName(String strZoneOutName) {
		return vPlanePlanRepository.findOutByZoneOutName(strZoneOutName);
	}

	/**
	 * 函数功能说明： 查询航班时刻 单程 往返一样
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人hanxl
	 * @date 创建日期 修改时间 2021.03.17
	 */
	public List<VPlanePlan> findSingle(String strOutCityName, String strInCityName, String strOutDate) {
		Date outDate = new Date();
		// 判断是否为空
		if (strOutCityName == null || strOutCityName == "") {
			strOutCityName = "北京市";
		}
		if (strInCityName == null) {
			strInCityName = "";
		}
		if (!(strOutDate == null || strOutDate == "")) {
			// 处理date
			Calendar calStartDate = Calendar.getInstance();
			calStartDate.setTime(common.formatDate(strOutDate, UtilConstants.DateStyle.YYYY_MM_DD));
			calStartDate.set(Calendar.HOUR_OF_DAY, 0);
			calStartDate.set(Calendar.MINUTE, 0);
			calStartDate.set(Calendar.SECOND, 0);
			calStartDate.set(Calendar.MILLISECOND, 1);
			outDate = calStartDate.getTime();

		}

		return vPlanePlanRepository.findSingle(strOutCityName, strInCityName, outDate);
	}

	/**
	 * 函数功能说明：前台 新建飞机票预留信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人hanxl
	 * @date 创建日期 修改时间 2021.3.17
	 */
	public VPlanePlan findByPlanID(int iPlanID) {
		// TODO Auto-generated method stub
		return vPlanePlanRepository.findByPlanID(iPlanID);
	}

	/**
	 * 后台招标管理中显示自己公司的招标信息如果为管理员显示全部
	 *
	 * @author 创建人 hanxl
	 * @param strPlaneID
	 * @date 创建日期 2021.02.22
	 */
	public List<VPlanePlan> findPlanePlan(TSysUser sessionSysUser, String strPlaneID) {
		int iOrgID = sessionSysUser.getOrgID();
		int iPlaneID = Integer.parseInt(strPlaneID);
		List<VPlanePlan> lstPlanePlan = new ArrayList<VPlanePlan>();
		if (sessionSysUser.getUserID() == 0) {
			lstPlanePlan = vPlanePlanRepository.findAll(iPlaneID);
		} else {
			lstPlanePlan = vPlanePlanRepository.findByOrgID(iOrgID, iPlaneID);
		}
		return lstPlanePlan;
	}

	/**
	 * 后台航班信息管理中显示自己公司的航班信息如果为管理员显示全部 异步加载表格
	 *
	 * @author 创建人 hanxl 整理 修改人hanxl
	 * @param strPlaneID
	 * @date 创建日期 2020.11.19 修改时间2021.02.22
	 */
	public List<VPlanePlan> findByOrgID(String strPlaneID, TSysUser sessionSysUser, String strPlaneOutDate,
			String strPlaneOutZone, String strPlaneInZone, int startIndex, int count) {
		int iPlaneID = Integer.parseInt(strPlaneID);
		if (strPlaneOutZone == null)
			strPlaneOutZone = "";
		if (strPlaneInZone == null)
			strPlaneInZone = "";
		int iOrgID = sessionSysUser.getOrgID();
		List<VPlanePlan> lstPlanePlan = new ArrayList<VPlanePlan>();
		Date outDate = new Date();
		if (!(strPlaneOutDate == null || strPlaneOutDate == "")) {
			// 处理date
			Calendar calStartDate = Calendar.getInstance();
			calStartDate.setTime(common.formatDate(strPlaneOutDate, UtilConstants.DateStyle.YYYY_MM_DD));
			calStartDate.set(Calendar.HOUR_OF_DAY, 0);
			calStartDate.set(Calendar.MINUTE, 0);
			calStartDate.set(Calendar.SECOND, 0);
			calStartDate.set(Calendar.MILLISECOND, 1);
			outDate = calStartDate.getTime();
			if (sessionSysUser.getUserID() == 0) {
				lstPlanePlan = vPlanePlanRepository.findSearchALLDate(iPlaneID, outDate, strPlaneOutZone,
						strPlaneInZone, startIndex, count);
			} else {
				lstPlanePlan = vPlanePlanRepository.findSearchByOrgIDDate(iPlaneID, iOrgID, outDate, strPlaneOutZone,
						strPlaneInZone, startIndex, count);
			}
		} else {
			if (sessionSysUser.getUserID() == 0) {
				lstPlanePlan = vPlanePlanRepository.findSearchALL(iPlaneID, strPlaneOutZone, strPlaneInZone, startIndex,
						count);
			} else {
				lstPlanePlan = vPlanePlanRepository.findSearchByOrgID(iPlaneID, iOrgID, strPlaneOutZone, strPlaneInZone,
						startIndex, count);
			}
		}
		return lstPlanePlan;
	}

	/**
	 * 后台航班信息管理中显示自己公司的航班信息如果为管理员显示全部 分页
	 *
	 * @author 创建人 hanxl 整理 修改人hanxl
	 * @param strPlaneID
	 * @date 创建日期 2020.11.19 修改时间2021.02.22
	 */
	public int queryPlanCount(String strPlaneID, TSysUser sessionSysUser, String strPlaneOutDate,
			String strPlaneOutZone, String strPlaneInZone) {
		int iPlaneID = Integer.parseInt(strPlaneID);
		Date outDate = new Date();
		if (strPlaneOutZone == null)
			strPlaneOutZone = "";
		if (strPlaneInZone == null)
			strPlaneInZone = "";
		int iOrgID = sessionSysUser.getOrgID();
		int count = 0;
		if (!(strPlaneOutDate == null || strPlaneOutDate == "")) {
			// 处理date
			Calendar calStartDate = Calendar.getInstance();
			calStartDate.setTime(common.formatDate(strPlaneOutDate, UtilConstants.DateStyle.YYYY_MM_DD));
			calStartDate.set(Calendar.HOUR_OF_DAY, 0);
			calStartDate.set(Calendar.MINUTE, 0);
			calStartDate.set(Calendar.SECOND, 0);
			calStartDate.set(Calendar.MILLISECOND, 1);
			outDate = calStartDate.getTime();
			if (sessionSysUser.getUserID() == 0) {
				count = vPlanePlanRepository.countALLDate(iPlaneID, outDate, strPlaneOutZone, strPlaneInZone);
			} else {
				count = vPlanePlanRepository.countByOrgIDDate(iPlaneID, iOrgID, outDate, strPlaneOutZone,
						strPlaneInZone);
			}
		} else {
			if (sessionSysUser.getUserID() == 0) {
				count = vPlanePlanRepository.countALL(iPlaneID, strPlaneOutZone, strPlaneInZone);
			} else {
				count = vPlanePlanRepository.countByOrgID(iPlaneID, iOrgID, strPlaneOutZone, strPlaneInZone);
			}
		}

		return count;
	}

	/**
	 * 后台创建航班时刻
	 *
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.03.17
	 */
	public void createPlanePlan(TPlanePlan tPlanePlan, String planStartDate, String planEndDate, String planWeekNum,
			String planStartTime, TSysUser sessionSysUser) {
		String[] weekDays = planWeekNum.split(",");
		String[] times = planStartTime.split(":");
		String strLong = tPlanePlan.getLong().toString();
		String[] timeLong = strLong.split("[.]");
		Long timeLongs = (long) ((Integer.parseInt(timeLong[0]) * 60 + Integer.parseInt(timeLong[1])) * 60 * 1000);
		List<Date> dateResultOut = new ArrayList<>();
		List<Date> dateResultIn = new ArrayList<>();
		Calendar cal = Calendar.getInstance();
		String[] dateInterval = { planStartDate, planEndDate };
		Date[] dates = new Date[dateInterval.length];
		for (int i = 0; i < dateInterval.length; i++) {
			String[] ymd = dateInterval[i].split("[^\\d]+");
			cal.set(Integer.parseInt(ymd[0]), Integer.parseInt(ymd[1]) - 1, Integer.parseInt(ymd[2]));
			dates[i] = cal.getTime();
		}
		for (Date date = dates[0]; date.compareTo(dates[1]) <= 0;) {
			cal.setTime(date);
			for (int j = 0; j < weekDays.length; j++) {
				if (cal.get(Calendar.DAY_OF_WEEK) - 1 == Integer.parseInt(weekDays[j])) {
					Date newdate = new Date();
					// 将日期和出发时间进行拼接
					Calendar calStartDate = Calendar.getInstance();
					calStartDate.setTime(date);
					calStartDate.set(Calendar.HOUR_OF_DAY, Integer.parseInt(times[0]));
					calStartDate.set(Calendar.MINUTE, Integer.parseInt(times[1]));
					calStartDate.set(Calendar.SECOND, Integer.parseInt(times[2]));
					calStartDate.set(Calendar.MILLISECOND, 1);
					newdate = calStartDate.getTime();

					dateResultOut.add(newdate);
					long timeIn = newdate.getTime(); // 得到指定日期的毫秒数
					timeIn += timeLongs;
					Date dateIn = new Date(timeIn);

					dateResultIn.add(dateIn);
				}
			}
			cal.add(Calendar.DATE, 1);
			date = cal.getTime();
		}

		for (int k = 0; k < dateResultOut.size(); k++) {
			TPlanePlan newPlanePlan = new TPlanePlan();
			// 获取计划最大编号
			int iPlanID = common.createPlanCode(UtilConstants.TableName.T_PLANE_PLAN);
			newPlanePlan.setPlanID(iPlanID);
			newPlanePlan.setStandard(tPlanePlan.getStandard());
			newPlanePlan.setPlaneID(tPlanePlan.getPlaneID());
			newPlanePlan.setZoneOutID(tPlanePlan.getZoneOutID());
			newPlanePlan.setAirportOutID(tPlanePlan.getAirportOutID());
			newPlanePlan.setZoneInID(tPlanePlan.getZoneInID());
			newPlanePlan.setAirportInID(tPlanePlan.getAirportInID());
			newPlanePlan.setStartDate(dateResultOut.get(k));
			newPlanePlan.setEndDate(dateResultIn.get(k));
			newPlanePlan.setLong(tPlanePlan.getLong());
			newPlanePlan.setFloor(tPlanePlan.getFloor());
			newPlanePlan.setPeer(tPlanePlan.getPeer());
			newPlanePlan.setPrice(tPlanePlan.getPrice());
			newPlanePlan.setNumber(tPlanePlan.getNumber());
			newPlanePlan.setMemo(tPlanePlan.getMemo());
			newPlanePlan.setCreateDate(new Date());
			newPlanePlan.setCreator(sessionSysUser.getUserName());
			newPlanePlan.setSpare1(String.valueOf(sessionSysUser.getOrgID()));
			tPlanePlanRepository.save(newPlanePlan.getPlanID());
			tPlanePlanRepository.save(newPlanePlan);
		}
	}

	/**
	 * 获取计划单最大编码
	 * 
	 * @return 返回值说明 计划单编码
	 * @author 创建人 dingxd
	 * @date 创建日期 2020.11.16
	 */
	public String getMaxPlanID() {
		// 返回计划单最大编码
		Object objCode = tPlanePlanRepository.getMaxPlanID();
		String strPlanID = "";
		if (objCode != null) {
			strPlanID = objCode.toString();
		}
		return strPlanID;
	}

	/**
	 * 函数功能说明： 修改航班时刻
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @param planEndDate
	 * @param planStartDate
	 * @date 创建日期
	 */

	public void updatePlanePlan(TPlanePlan tPlanePlan, String planStartDate, String planEndDate,
			TSysUser sessionSysUser) {

		BigDecimal fLong = common.getTimeBetweenTwoDay(planStartDate, planEndDate);
//		 定义一个新的实体类对象 
		TPlanePlan dbPlanePlan = tPlanePlanRepository.findByPlanID(tPlanePlan.getPlanID());
		dbPlanePlan.setStandard(tPlanePlan.getStandard());
		dbPlanePlan.setPlaneID(tPlanePlan.getPlaneID());
		dbPlanePlan.setZoneOutID(tPlanePlan.getZoneOutID());
		dbPlanePlan.setAirportOutID(tPlanePlan.getAirportOutID());
		dbPlanePlan.setZoneInID(tPlanePlan.getZoneInID());
		dbPlanePlan.setAirportInID(tPlanePlan.getAirportInID());
		Calendar calStartDate = Calendar.getInstance();
		calStartDate.setTime(common.formatDate(planStartDate, UtilConstants.DateStyle.YYYY_MM_DD_HH_MM_SS));
		dbPlanePlan.setStartDate(calStartDate.getTime());
		Calendar calEndDate = Calendar.getInstance();
		calEndDate.setTime(common.formatDate(planEndDate, UtilConstants.DateStyle.YYYY_MM_DD_HH_MM_SS));
		dbPlanePlan.setEndDate(calEndDate.getTime());
		dbPlanePlan.setLong(fLong);
		dbPlanePlan.setFloor(tPlanePlan.getFloor());
		dbPlanePlan.setPeer(tPlanePlan.getPeer());
		dbPlanePlan.setPrice(tPlanePlan.getPrice());
		dbPlanePlan.setNumber(tPlanePlan.getNumber());
		dbPlanePlan.setMemo(tPlanePlan.getMemo());
		dbPlanePlan.setModifyDate(new Date());
		dbPlanePlan.setReviser(sessionSysUser.getUserName());
		tPlanePlanRepository.save(dbPlanePlan);

	}
}
